﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;

public partial class Templates_News_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);

        //dsPressReleases.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsPressReleases_SelectData);
        //dlPressReleases.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlPressReleases_ItemDataBound);

        dsExhibitions.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsExhibitions_SelectData);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    //void dsPressReleases_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    //{
    //    e.SetData( AcmsContext.CurrentPost.Parent.Parent.FindChildByName("Press-Area").FindChildByName("Press-Releases").Children.OrderBy(x => x.Sequence).Take(3).ToList());
    //}



    void dsExhibitions_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Parent.FindChildByName("Exhibitions").FindChildByName("Trade-Shows").Children.OrderBy(x => x.Sequence).Take(1).ToList());
    }

    //void dlPressReleases_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    //{
    //    var post = e.DataItem as Post;
    //    if (post != null)
    //    {
    //        var lnkPdf = e.Item.FindControl("lnkPdf") as HyperLink;
    //        if (lnkPdf != null)
    //        {
    //            var pdf = post.FindPostControl("PdfArticle");// as ImageContentControlDisplay;
    //            var prop = pdf.GetProperties<Acms.AcmsControls.PdfContentControlProperties>();
    //            if (prop != null && prop.HasResource())
    //            {
    //                lnkPdf.NavigateUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
    //                lnkPdf.Text = post.DisplayName;
    //            }
    //        }
    //    }
    //}

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    

    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
}
